<?php
/**
 * Created by PhpStorm.
 * User: 17464
 * Date: 2020/1/2
 * Time: 17:23
 */

namespace App\Http\Transformers\Goods;

use App\Models\Attachment;
use App\Models\Goods;
use League\Fractal\TransformerAbstract;

class GetGoodsDetailTransformer extends TransformerAbstract
{
    public function transform(Goods $goods)
    {
        $data = $goods->toArray();

        if($data['headImage']){
            data_set($data, 'headImage', \GuzzleHttp\json_decode($data['headImage']));
        }

        if($data['logo']){
            data_set($data, 'logo', \GuzzleHttp\json_decode($data['logo']));
        }

        //获取图片数据
        $logo_attachment_data= Attachment::find($data['logo']);
        $data['logo_attachment_data'] = $logo_attachment_data ? $logo_attachment_data->toArray(): [];

        $head_image_attachment_data= Attachment::whereIn('id', $data['headImage'])->get();
        $data['head_image_attachment_data'] = $head_image_attachment_data ? $head_image_attachment_data->toArray(): [];
        return $data;
    }
}
